Page({
  data: {
    inputUsernameDate: "1234",
    inputPasswordDate: "1234",
    loginBtnTxt: "登录",
    btnLoading: false,
    disabled: false,
    inputUserName: '',
    inputPassword: '',
    avatarUrl: "../../staic/jinqian.png",
    logIcon: "../../staic/人物.png",
    pwdIcon: "../../staic/suoding.png",
    key: "1"
  },

  onLoad: function (options) {
    // 页面初始化 options 为页面跳转所带来的参数
  },

  onReady: function () {
    // 页面渲染完成
  },

  onShow: function () {
    // 页面显示
  },

  onHide: function () {
    // 页面隐藏
  },

  onUnload: function () {
    // 页面关闭
  },

  // 新增的跳转到注册页面的函数
  navigateToRegister: function () {
    wx.navigateTo({
      url: '/pages/another-existing-page/another-existing-page',
    });
  },
  
  // 登录成功后的跳转函数
  redirectTo: function (param) {
    param = JSON.stringify(param);
    wx.navigateTo({
      url: 'file:///D:/Code/58.html', // 登录成功后跳转的页面路径
    });
  },

  // 表单提交事件
  formSubmit: function (e) {
    var param = e.detail.value;
    this.mysubmit(param);
  },

  // 处理表单提交的逻辑
  mysubmit: function (param) {
    var flag = this.checkUserName(param) && this.checkPassword(param);
    if (flag) {
      this.setLoginData1();
      this.checkUserInfo(param);
    }
  },

  // 设置登录数据状态为登录中
  setLoginData1: function () {
    this.setData({ 
      loginBtnTxt: "登录中",
      disabled: !this.data.disabled,
      btnLoading: !this.data.btnLoading
    });
  },

  // 设置登录数据状态为登录
  setLoginData2: function () {
    this.setData({
      loginBtnTxt: "登录",
      disabled: !this.data.disabled,
      btnLoading: !this.data.btnLoading
    });
  },

  // 验证用户名
  checkUserName: function (param) {
    var inputUserName = param.username.trim();
    var inputPassword = param.password.trim();
    if (inputUserName.length >= 3 && inputPassword.length >= 3) {
      return true;
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确的账号密码'
      });
      return false;
    }
  },

  // 验证密码
  checkPassword: function (param) {
    var password = param.password.trim();
    if (password.length <= 0) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入密码'
      });
      return false;
    } else {
      return true;
    }
  },

  // 检查用户名和密码
  checkUserInfo: function (param) {
    var username = param.username.trim();
    var password = param.password.trim();
    var that = this;
    if (username == '123' && password == '123') {
      setTimeout(function () {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 1500
        });
        that.setLoginData2();
        that.redirectTo(param); // 登录成功后调用跳转函数
      }, 2000);
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '用户名或密码有误，请重新输入'
      });
      this.setLoginData2();
    }
  },

  // 处理注册页面的跳转点击事件
  onRegisterTap: function () {
    this.navigateToRegister(); // 调用注册跳转函数
  },

  // 处理找回密码页面的跳转点击事件
  onForgotPasswordTap: function () {
    wx.navigateTo({
      url: 'file:///D:/Code/58.html', // 找回密码页面的路径
    });
  }
});
